package com.ebt.m.customer.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.entity.CustomerDetail;
import com.ebt.m.customer.entity.CustomerSimple;
import com.ebt.m.data.CustomerData;
import com.ebt.m.data.DatabaseManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rick.Wang on 2016/12/7.
 */

public class CustomerDBManager {

    private static CustomerDBManager instance;

    private CustomerDBManager() {
    }

    public static CustomerDBManager getInstance(){
        if (instance == null) {
            synchronized (CustomerDBManager.class) {
                if (instance == null) {
                    instance = new CustomerDBManager();
                }
            }
        }
        return instance;
    }

    /**
     * 存储一个简单的对象
     */
    public void saveSimpleCustomer(Context context, CustomerDetail entity, String uuid){
        CustomerData customerData = new CustomerData(context);
        CustomerSimple simple = new CustomerSimple();
        simple.setCustomerId(uuid);
        simple.setPortraitPath(entity.getPortraitPath());
        simple.setName(entity.getName());
        simple.setSex(entity.getSex());
        simple.setTag(CustomerConstant.CustomerTag.TAG_0);
        customerData.insertObject(CustomerConstant.CustomerSimple.TABLE_NAME, simple);
    }

    public void clearTable(Context context, String tableName){
        CustomerData customerData = new CustomerData(context);
        customerData.clearTable(tableName);
    }

    public void saveSimpleCustomers(Context context, List<CustomerSimple> customerSimples){
        if (customerSimples == null ){
            return;
        }

        StringBuffer sql_insert_customer_simple = new StringBuffer();
        sql_insert_customer_simple.append("INSERT INTO " + CustomerConstant.CustomerSimple.TABLE_NAME + "("
                + CustomerConstant.CustomerSimple.COLUMN_ID + ","
                + CustomerConstant.CustomerSimple.COLUMN_NAME + ","
                + CustomerConstant.CustomerSimple.COLUMN_NAME_PREFIX + ","
                + CustomerConstant.CustomerSimple.COLUMN_PORTRAIT_PATH + ","
                + CustomerConstant.CustomerSimple.COLUMN_TAG + ","
                + CustomerConstant.CustomerSimple.COLUMN_SEX +")");
        sql_insert_customer_simple.append(" VALUES( ?, ?, ?, ?, ?, ?)");

        DatabaseManager dbManager = DatabaseManager.getInstance(context);
        dbManager.open();
        SQLiteDatabase db = dbManager.getSqliteDatabase();
        SQLiteStatement statement_customer_simple = db.compileStatement(sql_insert_customer_simple.toString());
        db.beginTransaction();
        try {

            for (CustomerSimple item : customerSimples){
                statement_customer_simple.bindString(1, item.getCustomerId() == null ? "" : item.getCustomerId());
                statement_customer_simple.bindString(2, item.getName() == null ? "" : item.getName());
                statement_customer_simple.bindString(3, item.getNamePrefix() == null ? "" : item.getNamePrefix());
                statement_customer_simple.bindString(4, item.getPortraitPath() == null ? "" : item.getPortraitPath());
                statement_customer_simple.bindLong(5, item.getTag() == null ? 0 : item.getTag());
                statement_customer_simple.bindLong(6, item.getSex() == null ? 0 : item.getSex());
                statement_customer_simple.executeInsert();
            }
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
        }
        dbManager.close();
    }

    public List<CustomerSimple> getSimpleCustomers(Context context){
        CustomerData cd = new CustomerData(context);
        return cd.getListOfCustomerSimple();
    }

    public long getBadgeViewNum(Context context){
        CustomerData cd = new CustomerData(context);
        return cd.getNumberOfContactForCustomerGetNew();
    }

    public List<String> getSimpleCustomerNames(Context context){
        List<CustomerSimple> list = getSimpleCustomers(context);
        List<String> strings = new ArrayList<>();
        for(CustomerSimple item : list){
            strings.add(item.getName());
        }
        return strings;
    }
}
